package negModel;

import negModelExcep.eParteNoFunciona;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Transmision extends PartePerformance{

	private int maxcambios;
	private int cambio;
	private boolean manual;
		
	public Transmision(){}
	
	public Transmision (int vidaUtil, double peso, float aporteVelMax,String detalle,AlgoPesos precio,boolean removible,int performance,int maxc, boolean sn){
		this.setMaxVU(vidaUtil);
		this.arreglar();
		this.setPeso(peso);
		this.setAportevmax(aporteVelMax);
		this.setDetalle(detalle);
		this.setPrecio(precio);
		this.setRemovible(removible);
		this.setPerformance(performance);
		
		this.maxcambios = maxc;
		this.manual = sn;
		this.cambio = 0;
				
	}
	
	public Transmision (Element xmlElement) {
		this.setMaxVU(Integer.parseInt(xmlElement.getAttribute("maxVU")));
		this.setVidaUtil(Integer.parseInt(xmlElement.getAttribute("vidaUtil")));
		this.setPeso(Double.parseDouble(xmlElement.getAttribute("peso")));
		this.setAportevmax(Float.parseFloat(xmlElement.getAttribute("aportevmax")));
		this.setDetalle(xmlElement.getAttribute("detalle"));
		this.setPrecio(new AlgoPesos(Double.parseDouble(xmlElement.getAttribute("precio"))));
		this.setRemovible(Boolean.parseBoolean(xmlElement.getAttribute("removible")));
		this.setPerformance(Integer.parseInt(xmlElement.getAttribute("performance")));
		
		this.maxcambios=Integer.parseInt(xmlElement.getAttribute("maxcambios"));
		this.cambio=Integer.parseInt(xmlElement.getAttribute("cambio"));
		this.manual=Boolean.parseBoolean(xmlElement.getAttribute("manual"));
	}
	
	public Element toXml(Document doc) {
		Element xmlElement = doc.createElement("parte");
		xmlElement.setAttribute("Clase","transmision");
		
		xmlElement.setAttribute("maxVU", ""+ this.getMaxVU());
		xmlElement.setAttribute("vidaUtil", ""+this.getVidaUtil());
		xmlElement.setAttribute("peso", ""+this.getPeso());
		xmlElement.setAttribute("aportevmax", ""+ this.aportevmax());
		xmlElement.setAttribute("detalle", this.getDetalle());
		xmlElement.setAttribute("precio", ""+(this.getPrecio()).getMonto());
		xmlElement.setAttribute("removible", ""+this.esRemovible());
		xmlElement.setAttribute("performance", ""+this.getPerformance());
		
		xmlElement.setAttribute("maxcambios", ""+this.maxcambios);
		xmlElement.setAttribute("cambio", ""+this.cambio);
		xmlElement.setAttribute("manual", ""+this.manual);
				
		return xmlElement;
	}
	
	public double funcionar() throws eParteNoFunciona {
		this.gastar();
		return cambio; 
	}
	
	public boolean subirCambio(){
		
		if (this.esManual()){
			if (cambio < maxcambios){
				cambio++;
				return true;
			}
			else 
				return false;
		}
		else
			return false;
	}
	
	public boolean bajarCambio(){
		
		if (this.esManual()){
			if (cambio > 0){
				cambio--;
				return true;
			}
			else
				return false;
		}
		else
			return false;
	}
	
	public int getCambio(){
		return cambio;
	}
	
	public int getMaxCambios(){
		return maxcambios;
	}
	
	public boolean esManual(){
		return manual;
	}
	
	public void setCambio(int cambio){
		this.cambio = cambio;
	}
	
	public void setCambio(int rpm, double vel ){
				
		int rpmxcambio=(int) (vel/this.maxcambios);
		
		if(rpm<=rpmxcambio) this.cambio=1;
		
		for(int i=1;i<maxcambios;i++){
			if((rpm>=rpmxcambio*i)&&(rpm<=rpmxcambio*(i+1)))
				this.cambio=i+1;
		}
		
	}
	
	public void agregarMejora(Vehiculo v) {
		
		super.agregarMejora(v);
		((Auto)v).setTransmision(this);
		
	}

}
